package org.jmoyer.NotificationPlus;

import org.jmoyer.NotificationPlus.R;

import android.content.Context;
import android.content.DialogInterface;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.NumberPicker;

public class IntervalPickerPreference extends DialogPreference {
	private Context mContext;
    private int currentHour = 0; // 0-24
    private int currentMinute = 0; // 0-59
    private int currentSecond = 0; // 0-59

	public IntervalPickerPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		initialize(context);
	}

	public IntervalPickerPreference(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initialize(context);
	}

	private void initialize(Context context) {
		mContext = context;
		
		String pref = mContext.getSharedPreferences(context.getString(R.string.PREFS_FILE), Context.MODE_PRIVATE).getString(getKey(), "0:1:0");
		currentHour = Integer.valueOf(pref.split(":")[0]);
		currentMinute = Integer.valueOf(pref.split(":")[1]);
		currentSecond = Integer.valueOf(pref.split(":")[2]);
	}

	@Override
	protected View onCreateDialogView()
	{
		TableLayout layout = new TableLayout(mContext);
		//layout.setPadding(6, 6, 6, 6);

		NumberPicker mPickHour = new NumberPicker(mContext);
		mPickHour.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                currentHour = newVal;
            }
        });
		mPickHour.setMinValue(0);		
		mPickHour.setMaxValue(24);
		mPickHour.setValue(currentHour);
		
		NumberPicker mPickMinute = new NumberPicker(mContext);
		mPickMinute.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
				currentMinute = newVal;
            }
        });
		mPickMinute.setMinValue(0);
		mPickMinute.setMaxValue(59);
		mPickMinute.setValue(currentMinute);
		
		NumberPicker mPickSecond = new NumberPicker(mContext);
		mPickSecond.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
				currentSecond = newVal;
            }
        });
		mPickSecond.setMinValue(0);
		mPickSecond.setMaxValue(59);
		mPickSecond.setValue(currentSecond);
		
		TextView textHour = new TextView(mContext);
		textHour.setText("h");

		TextView textMinute = new TextView(mContext);
		textMinute.setText("m");

		TextView textSecond = new TextView(mContext);
		textSecond.setText("s");

		TableRow row_one = new TableRow(mContext);
		row_one.setGravity(Gravity.CENTER);
		row_one.addView(mPickHour);
		row_one.addView(textHour);
		row_one.addView(mPickMinute);
		row_one.addView(textMinute);
		row_one.addView(mPickSecond);
		row_one.addView(textSecond);

		layout.addView(row_one);

		return layout;
	}

	public void onClick(DialogInterface dialog, int which) {
		switch (which) {
		case DialogInterface.BUTTON_POSITIVE:
			saveValue(getValue());
			break;
		default:
			break;
		}
	}

	private void saveValue(String val) {
		getEditor().putString(getKey(), val).commit();
		notifyChanged();
	}

	public String getValue() {
		return currentHour + ":" + currentMinute + ":" + currentSecond;
	}

}
